iT邦幫忙

0

Day 8: 58. Length of Last Word

  • 分享至 

  • xImage
  •  

Given a string s consisting of words and spaces, return the length of the last word in the string.

A word is a maximal substring consisting of non-space characters only.

Example 1:
Input: s = "Hello World"
Output: 5
Explanation: The last word is "World" with length 5.
Example 2:
Input: s = " fly me to the moon "
Output: 4
Explanation: The last word is "moon" with length 4.
Example 3:
Input: s = "luffy is still joyboy"
Output: 6
Explanation: The last word is "joyboy" with length 6.
解題思路
1.使用trim()去掉前後空白。
2.找出最後一個空白的位置。
3.用字串長度減掉最後空白位置即可得最後一個單字長度。
https://ithelp.ithome.com.tw/upload/images/20251012/20179424aPBv55hSbR.png


圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言